package org.open.cobertura.cli;

import org.open.cobertura.core.analyzer.CoberturaAnalyzer;
import org.open.cobertura.core.output.IOutputClient;
import org.open.cobertura.core.output.factory.AbstractOutputClient;
import org.open.cobertura.core.output.factory.TCPOutputClientFactory;
import org.open.cobertura.enums.CommonParams;
import org.open.cobertura.util.LoggingUtil;

import java.util.logging.Logger;

public class CoberturaCli {
    private final static Logger logger = Logger.getLogger(CoberturaCli.class.getName());
    public static void main(String[] args) {
        LoggingUtil.setLogging();
        if(!args[0].equals("-report")){
            logger.info("第0个参数需要输入-report生成报告参数");
            return;
        }
        if(!args[1].equals("-host")){
            logger.info("第1个参数需要输入-host端口参数，示例ip:port");
            return;
        }
        if(!args[3].equals("-source")){
            logger.info("第2个参数需要输入-source源码参数");
            return;
        }
        if(!args[5].equals("-classes")){
            logger.info("第3个参数需要输入classes字节码目录参数");
            return;
        }
        CoberturaCli coberturaCli=new CoberturaCli();
        coberturaCli.executor(args);
    }

    /**
     * 执行命令
     * @param args
     */
    private void executor(String[] args){
        CommandLineBuilder commandLineBuilder=new CommandLineBuilder(args);
        AbstractOutputClient abstractOutputClient=new TCPOutputClientFactory();
        IOutputClient outputClient=abstractOutputClient.createOutputClient();
        outputClient.dumpRuntimeData(commandLineBuilder.getHost(),commandLineBuilder.getPort());
        CoberturaAnalyzer coberturaAnalyzer=new CoberturaAnalyzer();
        coberturaAnalyzer.createCoberturaReport(CommonParams.coberturaDataFile.getValue(),commandLineBuilder.getClasses(),commandLineBuilder.getSource());
    }
}
